<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Hdlotterytimemodel extends CI_Model {

    protected $_CI;

    public static $searchField = array(
        'lottery_time_date_start' => 'lottery_time_date >=',
        'lottery_time_date_end' => 'lottery_time_end >=',
        'lottery_time_id' => 'lottery_time_id',
        
    );
    public static $likeField = array(
    );

    public static $sortField = array(
        'lottery_time_date' => 'lottery_time_date',
        'lottery_time_start' => 'lottery_time_start',
        'lottery_time_end' => 'lottery_time_end',
        'lottery_time_gift1_count' => 'lottery_time_gift1_count',
        'lottery_time_gift1_count_drawn' => 'lottery_time_gift1_count_drawn',
        'lottery_time_gift2_count' => 'lottery_time_gift2_count',
        'lottery_time_gift2_count_drawn' => 'lottery_time_gift2_count_drawn',
        'lottery_time_gift3_count' => 'lottery_time_gift3_count',
        'lottery_time_gift3_count_drawn' => 'lottery_time_gift3_count_drawn',
        'lottery_time_gift4_count' => 'lottery_time_gift4_count',
        'lottery_time_gift4_count_drawn' => 'lottery_time_gift4_count_drawn',
        'lottery_time_gift5_count' => 'lottery_time_gift5_count',
        'lottery_time_gift5_count_drawn' => 'lottery_time_gift5_count_drawn',
        'lottery_time_gift6_count' => 'lottery_time_gift6_count',
        'lottery_time_gift6_count_drawn' => 'lottery_time_gift6_count_drawn',
        'lottery_time_gift7_count' => 'lottery_time_gift7_count',
        'lottery_time_gift7_count_drawn' => 'lottery_time_gift7_count_drawn',
    );

    public function __construct(){
        parent::__construct();
        $this->_CI = & get_instance();
    }
/*
| -------------------------------------------------------------------
|  GET Functions
| -------------------------------------------------------------------
*/
    public function getLotteryTimeListSection($assist){
        $this->db->from('hd_game_lottery_time');
        $this->db->where('lottery_time_status !=',STATUS_DELETE);
        if(!empty($assist['where'])) $this->db->where($assist['where']);
        if(!empty($assist['like'])) $this->db->like($assist['like']);
        $count = $this->db->count_all_results('',SQL_EXECUTE_RETAIN_CONDITION);

        if(!empty($assist['sort']) && !empty($assist['sort'])) $this->db->order_by($assist['sort']['key'],$assist['sort']['dir']);
        $this->db->order_by('lottery_time_id','desc');
        if(!empty($assist['pagination'])) $this->db->limit($assist['pagination']['limit'],$assist['pagination']['start']);
        $query = $this->db->get();
        $list = $query->result_array();

        return array($list,$count);
    }

    public function getLotteryTime($lottery_time_id){
        $this->db->from('hd_game_lottery_time');
        $this->db->where('lottery_time_id',$lottery_time_id);
        $this->db->where('lottery_time_status !=',STATUS_DELETE);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }

    public function getCurrentLotteryTimeRate() {
        $t = date('H:i:s');
        $this->db->from('hd_game_lottery_time');
        $this->db->where('lottery_time_date',date('Y-m-d'));
        $this->db->where('lottery_time_start <=',$t);
        $this->db->where('lottery_time_end >',$t);
        $this->db->where('lottery_time_status !=',STATUS_DELETE);
        $this->db->limit(1);
        $query = $this->db->get();
        $res = $query->row_array();

        return $res;
    }
/*
| -------------------------------------------------------------------
|  CREATE Functions
| -------------------------------------------------------------------
*/
    public function createLotteryTime($info){
        $this->db->insert('hd_game_lottery_time',$info);
        $lottery_time_id = $this->db->insert_id();
        $this->db->query('insert into {PRE}hd_game_lottery_time_history select null,a.* from {PRE}hd_game_lottery_time as a where lottery_time_id='.$lottery_time_id);
        return $lottery_time_id;
    }

    public function createLotteryTimeContentBatch($info){
        if(!empty($info)) $this->db->insert_batch('hd_game_lottery_time',$info);
    }

/*
| -------------------------------------------------------------------
|  UPDATE Functions
| -------------------------------------------------------------------
*/
    public function updateLotteryTime($info){
        $this->db->where('lottery_time_id',$info['lottery_time_id']);
        $lottery_time_id = $info['lottery_time_id'];
        unset($info['lottery_time_id']);
        $this->db->update('hd_game_lottery_time',$info);
        $this->db->query('insert into {PRE}hd_game_lottery_time_history select null,a.* from {PRE}hd_game_lottery_time as a where lottery_time_id='.$lottery_time_id);
        return true;
    }

    public function updateLotteryTimeBatch($info){
        if(!empty($info)) $this->db->update_batch('hd_game_lottery_time',$info,'lottery_time_id');
    }

    public function updateLotteryCountDrawn($lottery_time_id,$gift_type) {
        $this->db->query('update {PRE}hd_game_lottery_time a set lottery_time_gift'.$gift_type.'_count_drawn=(select count(*) 
                from {PRE}hd_game_lottery_history where lottery_time_id=a.lottery_time_id and gift_type = '.$gift_type.') 
                WHERE a.lottery_time_id='.$lottery_time_id);
    }

    public function editLotteryTime($info){
        if(isset($info['lottery_time_id'])) {
            $lottery_time_id = $info['lottery_time_id'];
            unset($info['lottery_time_id']);
            $this->db->where('lottery_time_id',$lottery_time_id);
            $this->db->update('hd_game_lottery_time',$info);
        }
    }
}